package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 查询品牌实现类
 * @author sym
 * @version V1.0
 * @Project java2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/21 18:53
 */
@Service
public class BrandServiceImpl implements  BrandService {


    @Autowired
    private DtsBrandMapper brandMapper;

    //阿里云北京服务器地址
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //子账户账号
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    //子账户密码
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    //bucket桶空间名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Override
    public IPage<DtsBrand> queryBrandPage(Integer id, String name, Integer page, Integer limit, String sort, String order) {

        //1、创建分页对象
        IPage iPage = new Page(page,limit);

        //2、封装查询对象
        QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        //判断查询条件是否为空
        if (id != null){
            queryWrapper.eq("id",id);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }
        //判断排序字段、排序方式是否为空
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        }

        //3、查询并返回
        IPage<DtsBrand> resultPage = brandMapper.selectPage(iPage, queryWrapper);
        return resultPage;
    }

    /**
     * 修改
     * @param brand
     * @return
     */
    @Override
    public int update(DtsBrand brand) {
        //设置修改时间
        brand.setUpdateTime(new Date());
        int count = brandMapper.updateById(brand);
        return count;
    }


    /**
     * 删除
     * @param
     */
    @Override
    public int delete(DtsBrand brand) {

        int Count = brandMapper.deleteById(brand.getId());
        return Count;
    }

    @Override
    public int add(DtsBrand brand) {
        int count = brandMapper.insert(brand);
        return count;
    }
}
